﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace vite_core.Common.Extensions
{
    public static class StringExtensions
    {
        #region 扩展String IndexOf
        /// <summary>
        /// 扩展String IndexOf
        /// </summary>
        /// <param name="_">字符串</param>
        /// <param name="value">子字符串</param>
        /// <returns></returns>
        public static int IndexOfX(this string _, string value) =>
#if NETSTANDARD2_1
            _.AsSpan().IndexOf(value.AsSpan());
#else
            _.IndexOf(value);
#endif
        #endregion

        #region 扩展String LastIndexOf
        /// <summary>
        /// 扩展String LastIndexOf
        /// </summary>
        /// <param name="_">字符串</param>
        /// <param name="value">子字符串</param>
        /// <returns></returns>
        public static int LastIndexOfX(this string _, string value) =>
#if NETSTANDARD2_1
            _.AsSpan().LastIndexOf(value.AsSpan());
#else
            _.LastIndexOf(value);
#endif
        #endregion

        #region 扩展String Substring
        /// <summary>
        /// 扩展String Substring
        /// </summary>
        /// <param name="_">字符串</param>
        /// <param name="start">开始位置</param>
        /// <param name="length">结束位置</param>
        /// <returns></returns>
        public static string SubstringX(this string _, int start, int length)
        {
            if (length <= 0 || length < start) return string.Empty;
            return
#if NETSTANDARD2_1
            _.AsSpan().Slice(start, length).ToString()
#else
            _.Substring(start, length)
#endif
                ;
        }
        #endregion

        #region 扩展String Substring
        /// <summary>
        /// 扩展String Substring
        /// </summary>
        /// <param name="_">字符串</param>
        /// <param name="start">开始位置</param>
        /// <returns></returns>
        public static string SubstringX(this string _, int start) => _.SubstringX(start, _.Length - start);
        #endregion

        #region 扩展String Replace
        /// <summary>
        /// 扩展String Replace
        /// </summary>
        /// <param name="_">字符串</param>
        /// <param name="oldValue">子字符串</param>
        /// <param name="newValue">替换后子符串</param>
        /// <returns></returns>
        public static string ReplaceX(this string _, string oldValue, string newValue)
        {
#if NETSTANDARD2_1
            var list = new List<string>();
            var strSpan = _.AsSpan();
            var subSpan = oldValue.AsSpan();
            var n = strSpan.IndexOf(subSpan);
            while (n > -1)
            {
                list.Add(strSpan.Slice(0, n).ToString());
                strSpan = strSpan.Slice(n + subSpan.Length);
                n = strSpan.IndexOf(subSpan);
            }
            return list.Join(newValue);
#else
            return _.Replace(oldValue, newValue);
#endif
        }
        #endregion

        public static bool IsNullOrEmpty(this string str)
        {
            return str.IsNullOrEmpty() ? true : false;
        }
    }
}
